package com.hhh.springai_test.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhh.springai_test.common.BaseResponse;
import com.hhh.springai_test.common.ResultUtils;
import com.hhh.springai_test.constant.UserConstant;
import com.hhh.springai_test.model.dto.comment.AddCommentDTO;
import com.hhh.springai_test.model.dto.comment.QueryCommentDTO;
import com.hhh.springai_test.model.dto.comment.UpdateCommentDTO;
import com.hhh.springai_test.model.dto.comment.SimpleQueryCommentDTO;
import com.hhh.springai_test.model.po.Comment;
import com.hhh.springai_test.model.vo.CommentVO;
import com.hhh.springai_test.service.CommentService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Resource
    private CommentService commentService;

    @PostMapping("/add")
    @SaCheckRole(UserConstant.DEFAULT_ROLE)
    public BaseResponse<Long> addComment(@RequestBody AddCommentDTO addCommentDTO) {
        String loginId = (String) StpUtil.getLoginId();
        Long userId = Long.valueOf(loginId);
        Long commentId = commentService.addComment(addCommentDTO, userId);
        return ResultUtils.success(commentId);
    }

    @PostMapping("/delete/{id}")
    @SaCheckRole(UserConstant.DEFAULT_ROLE)
    public BaseResponse<Boolean> deleteComment(@PathVariable Long id) {
        String loginId = (String) StpUtil.getLoginId();
        Long userId = Long.valueOf(loginId);
        boolean result = commentService.deleteComment(id, userId);
        return ResultUtils.success(result);
    }

    @PostMapping("/update")
    @SaCheckRole(UserConstant.DEFAULT_ROLE)
    public BaseResponse<Boolean> updateComment(@RequestBody UpdateCommentDTO updateCommentDTO) {
        String loginId = (String) StpUtil.getLoginId();
        Long userId = Long.valueOf(loginId);
        boolean result = commentService.updateComment(updateCommentDTO, userId);
        return ResultUtils.success(result);
    }

    @PostMapping("/list")
    public BaseResponse<Page<CommentVO>> listCommentPage(@RequestBody QueryCommentDTO queryCommentDTO) {
        Page<CommentVO> commentPage = commentService.queryCommentPage(queryCommentDTO);
        return ResultUtils.success(commentPage);
    }

    @PostMapping("/simple/list")
    public BaseResponse<Page<CommentVO>> simpleListCommentPage(@RequestBody SimpleQueryCommentDTO simpleQueryCommentDTO) {
        Page<CommentVO> commentPage = commentService.queryCommentSimplePage(simpleQueryCommentDTO);
        return ResultUtils.success(commentPage);
    }
}
